---
category: Misc
created: '2020-05-03'
title: Encode a URL
updated: '2021-10-13'
---

**JavaScript version**

```js encode.js
// `encodeURIComponent` doesn't encode -_.!~*'()
const encode = (url) =>
    encodeURIComponent(url)
        .replace(/!/g, '%21')
        .replace(/~/g, '%7E')
        .replace(/\*/g, '%2A')
        .replace(/'/g, '%27')
        .replace(/\(/g, '%28')
        .replace(/\)/g, '%29')
        .replace(/%20/g, '+');
```

**TypeScript version**

```ts encode.ts
const encode = (url: string): string =>
    encodeURIComponent(url)
        .replace(/!/g, '%21')
        .replace(/~/g, '%7E')
        .replace(/\*/g, '%2A')
        .replace(/'/g, '%27')
        .replace(/\(/g, '%28')
        .replace(/\)/g, '%29')
        .replace(/%20/g, '+');
```
